package com.community.demo.controller;

import com.community.demo.dao.BlogRepository;
import com.community.demo.domain.Blog;
import com.community.demo.domain.Goods;
import com.community.demo.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class BlogController {

    @Autowired
    private IBlogService blogService;

    @GetMapping("/blogList")
    public ModelAndView blogList(){
        List<Blog> blogs = blogService.findAll();
        return new ModelAndView("redirect:/blog", "blogs", blogs);
    }

    @PostMapping("/addBlog")
    public String addBlog(Blog blog){
        blogService.saveAndFlush(blog);
        return "/blog";
    }

    @PutMapping("/updateBlog")
    public String updateBlog(Blog blog){
        blogService.saveAndFlush(blog);
        return "/blog";
    }

    @PutMapping("/deleteBlog")
    public String deleteBlog(long id){
        blogService.deleteById(id);
        return "/blog";
    }

    @GetMapping("/getBlogById")
    public ModelAndView getBlogById(long id){
        Blog blog = blogService.getBlogById(id);
        return new ModelAndView("/blog", "blog", blog);
    }

}
